import sys
import os
import json
import codecs
import re


def preprocess_candidate(input_token: str) -> str:
    return_string: str = input_token.strip()
    return_string = return_string.lstrip(' ')
    return_string = return_string.rstrip(' ')
    return_string = re.sub(r' ', '', return_string)
    return return_string


if __name__ == '__main__':
    instance_file = sys.argv[1] if len(
        sys.argv) > 1 else 'replace_examples.txt'
    maptable_file = 'map_table.json' if len(sys.argv) > 2 else 'map_table.json'
    maptable = dict()
    with codecs.open(instance_file, 'r', 'utf-8') as f:
        lines = f.readlines()
        # every line is a list, read it as list
        for line in lines:
            mapping_list = json.loads(line.strip())
            for element in mapping_list:
                src_word = preprocess_candidate(element['src_word'])
                tgt_word = preprocess_candidate(element['tgt_word'])

                if tgt_word not in maptable:
                    maptable[tgt_word] = list()
                    if src_word is not "":
                        maptable[tgt_word].append(
                            src_word)
                else:
                    if src_word is not "":
                        maptable[tgt_word].append(
                            src_word)
    for key, value in maptable.items():
        temp = set(value)
        if key in temp:
            temp.remove(key)
        maptable[key] = list(temp)
    with codecs.open(maptable_file, 'w', 'utf-8') as f:
        f.write(json.dumps(maptable, ensure_ascii=False, indent=4))
    print("maptable has been saved in {}.".format(maptable_file))
